//
//Copyright GX. Yuan
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//   http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

/**
 * @file net.go
 * @brief Leetcode practice
 * @details 34. 在排序数组中查找元素的第一个和最后一个位置
 * @author GX. Yuan  any question please send mail to yuanguanxu@qq.com
 * @date 2021-6-28
 * @version V1.0
 * @attention 硬件平台: windows 10 家庭版
 * SDK版本：Go 1.16.5
 * IDE版本：GoLand 2020.2.3
 */

package main

import "fmt"

func searchRange(nums []int, target int) []int {
	left:= 0
	right:=len(nums)-1
	var mid int
	for left<=right {
		mid =(left+right)/2
		if nums[mid]==target {
			var max int
			var min int
			for min=mid; min>=0 && nums[mid]==nums[min];min--{
			}
			for max =mid;max<len(nums) && nums[mid]==nums[max];max++{
			}
			return []int{min+1,max-1}
		}
		if target < nums[mid]{
			right=mid-1
		}else{
			left=mid+1
		}
	}
	return []int{-1,-1}
}
func main() {
	//nums:=[]int{5,7,7,8,8,10}
	nums:=[]int{1}
	target:=1
	fmt.Println(searchRange(nums,target))
}

// 使用二分法，查找位置;
// 1. 数组是大小有序的；
// 2. 数组中的值的虽然重复，但是是连续的；
// 思路
// 利用二分法，找到需要的值
// 然后，更具值向两边衍生找到最大位置和最小位置的值